package com.boxing.dao;

import com.boxing.beans.Car;
import com.boxing.beans.CarResult;

import java.util.List;

public class CarDao extends BaseDao{

    /**
     * @return 1:成功；  2：原来没有，添加失败； 3：修改数量失败
     */
    public int addBook(Car car) {
        // 如果购物车中没有该商品，则添加；如果购物车中已经有了该商品，则将商品数量+1
        Object object = queryValue("select num from t_car where book_id=?", car.getBook_id());
        System.out.println("======"+object);
        if (object == null){
            int result = insertData("insert into t_car(email,book_id,num) values(?,?,?)", car.getEmail(),car.getBook_id(),1);
            if (result > 0){
                return 1;
            }else {
                return 2;
            }
        }else {
            int value = (int)object;
            int result = insertData("update t_car set num=? where book_id=?", ++value, car.getBook_id());
            if (result > 0){
                return 1;
            }else {
                return 3;
            }
        }

    }

    public List<Car> getCar() {
        return queryList("select email,book_id,num from t_car", Car.class);
    }

    public List<CarResult.CarItem> getCarAllBooks(){
        return queryList("select email,book_id,num,name as bookName,price,img from t_car JOIN t_book on t_car.book_id=t_book.id", CarResult.CarItem.class);
    }
}
